
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .header {
            text-align: center;
            margin-bottom: 30px;
            padding-bottom: 20px;
            border-bottom: 1px solid #eee;
        }
        .current-temp {
            font-size: 36px;
            font-weight: bold;
            color: #333;
            text-align: center;
            margin: 20px 0;
        }
        .temp-unit {
            font-size: 18px;
        }
        #temperatureChart {
            width: 100%;
            height: 400px;
            min-height: 400px;
            max-height: 400px;
            margin: 20px 0;
        }
        .status {
            text-align: center;
            font-size: 18px;
            margin: 10px 0;
        }
        .status.normal {
            color: #52c41a;
        }
        .status.warning {
            color: #faad14;
        }
        .status.danger {
            color: #ff4d4f;
        }
        .info-panel {
            display: flex;
            justify-content: space-around;
            margin: 20px 0;
            flex-wrap: wrap;
        }
        .info-item {
            text-align: center;
            padding: 15px;
            background-color: #f9f9f9;
            border-radius: 5px;
            min-width: 150px;
            margin: 5px;
        }
        .info-label {
            font-size: 14px;
            color: #666;
        }
        .info-value {
            font-size: 24px;
            font-weight: bold;
            margin-top: 5px;
        }
        .cpu-info {
            text-align: center;
            margin: 15px 0;
            padding: 10px;
            background-color: #e8f4fd;
            border-radius: 5px;
        }
    </style>

    <div class="container">
        
        <div class="current-temp">
            <span id="currentTemp">--</span>
            <span class="temp-unit">°C</span>
        </div>
        
        <div class="status" id="status">
            正在获取数据...
        </div>
        
        <div class="info-panel">
            <div class="info-item">
                <div class="info-label">最高温度</div>
                <div class="info-value"><span id="maxTemp">--</span>°C</div>
            </div>
            <div class="info-item">
                <div class="info-label">最低温度</div>
                <div class="info-value"><span id="minTemp">--</span>°C</div>
            </div>
            <div class="info-item">
                <div class="info-label">平均温度</div>
                <div class="info-value"><span id="avgTemp">--</span>°C</div>
            </div>
        </div>
        
        <div>
            <canvas id="temperatureChart"></canvas>
        </div>
    </div>

    <!-- 确保jQuery库已加载 -->

    <script src="/cpu_temperature/static/js/chart.min.js"></script>
    <script>
        // 等待文档加载完成
        $(document).ready(function() {

            // 图表数据
            const chartData = {
                labels: [],
                datasets: [{
                    label: 'CPU温度 (°C)',
                    data: [],
                    borderColor: 'rgb(75, 192, 192)',
                    backgroundColor: 'rgba(75, 192, 192, 0.2)',
                    tension: 0.1,
                    fill: true
                }]
            };

            // 图表配置
            const config = {
                type: 'line',
                data: chartData,
                options: {
                    responsive: true,
                    maintainAspectRatio: false,
                    animation: {
                        duration: 500,
                        easing: 'easeOutQuart'
                    },
                    scales: {
                        y: {
                            beginAtZero: false,
                        suggestedMin: 30,
                        suggestedMax: 80,
                        ticks: {
                            stepSize: 5
                        }
                        },
                        x: {
                            display: true
                        }
                    },
                    plugins: {
                        legend: {
                            display: true,
                            position: 'top',
                        },
                        tooltip: {
                            mode: 'index',
                            intersect: false
                        }
                    }
                }
            };

            // 创建图表
            let temperatureChart = null;


            const ctxElement = document.getElementById('temperatureChart');

            const ctx = ctxElement.getContext('2d');

            temperatureChart = new Chart(ctx, config);

            // 温度统计数据
            let temperatures = [];
            let maxTemp = 0;
            let minTemp = 100;
            let sumTemp = 0;
            let count = 0;
            
            // 定义定时器变量
            let updateInterval = null;

            // 更新温度数据
            function updateTemperature() {
                let win=$('#currentTemp').text();
                if(!win){
                    stopUpdateTimer();
                    return;
                }
                $.get('/plugin?action=a&s=get_temperature_data&name=cpu_temperature', function(data) {

                    if (!data.hasOwnProperty("temperature")) {
                        document.getElementById('status').textContent = '获取温度数据失败';
                        return;
                    }
                    
                    const temp = data.temperature;
                    const time = new Date(data.time * 1000).toLocaleTimeString();
                    
                    // 更新当前温度显示
                    document.getElementById('currentTemp').textContent = temp;
                    
                    // 更新状态显示
                    const statusElement = document.getElementById('status');
                    if (temp < 60) {
                        statusElement.textContent = '温度正常';
                        statusElement.className = 'status normal';
                    } else if (temp < 75) {
                        statusElement.textContent = '温度偏高';
                        statusElement.className = 'status warning';
                    } else {
                        statusElement.textContent = '温度过高';
                        statusElement.className = 'status danger';
                    }
                    
                    // 更新统计数据
                    if (typeof temp === 'number') {
                        temperatures.push(temp);
                        if (temperatures.length > 20) {
                            temperatures.shift(); // 只保留最近20个数据点
                        }
                        
                        // 更新统计信息
                        if (temp > maxTemp) maxTemp = temp;
                        if (temp < minTemp) minTemp = temp;
                        sumTemp += temp;
                        count++;
                        const avgTemp = sumTemp / count;
                        
                        // 更新页面显示
                        document.getElementById('maxTemp').textContent = maxTemp.toFixed(1);
                        document.getElementById('minTemp').textContent = minTemp.toFixed(1);
                        document.getElementById('avgTemp').textContent = avgTemp.toFixed(1);
                        
                        // 更新图表数据
                        chartData.labels.push(time);
                        chartData.datasets[0].data.push(temp);
                        
                        // 保持图表数据在合理范围内
                        if (chartData.labels.length > 20) {
                            chartData.labels.shift();
                            chartData.datasets[0].data.shift();
                        }
                        

                        if (temperatureChart) {
                            temperatureChart.update();
                        }

                    }
                })
            }

            function stopUpdateTimer() {
                if (updateInterval) {
                    clearInterval(updateInterval);
                    updateInterval = null;
                    console.log('温度更新定时器已停止');
                }
            }
            function startUpdateTimer() {
                // 如果定时器已经在运行，先停止它
                stopUpdateTimer();
                // 启动新的定时器
                updateInterval = setInterval(updateTemperature, 2000);
                console.log('温度更新定时器已启动');
            }
            // 初始化

            updateTemperature();
            
            // 启动定时更新，每2秒更新一次
            startUpdateTimer();
        });

    </script>